import entities.AdminUser;
import entities.User;
import services.LibraryService;
import services.UserService;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        UserService userService = new UserService();
        LibraryService libraryService = new LibraryService();
        Scanner scanner = new Scanner(System.in);

        while(true){
            User user = userService.login();
            if(user == null) continue;

            user.showMenu();
            while(true){
                System.out.println("请选择操作（或输入'退出'结束当前会话）：");
                String input = scanner.nextLine().trim().toLowerCase();

                if("退出".equals(input)){
                    System.out.println("感谢使用图书馆系统！");
                    break;
                }

                try{
                    int choice = Integer.parseInt(input);
                    switch(choice){
                        case 1:
                            System.out.println("请输入书名：");
                            String title = scanner.nextLine().trim();
                            System.out.print(libraryService.findBook(title));
                            break;
                        case 2:
                            if(user instanceof AdminUser){
                                addBook(scanner, libraryService);
                            }else{
                                borrowBook(scanner, libraryService);
                            }
                            break;
                        case 3:
                            if(user instanceof AdminUser){
                                removeBook(scanner, libraryService);
                            }else{
                                returnBook(scanner, libraryService);
                            }
                            break;
                        case 4:
                            if (user instanceof AdminUser) {
                                listAllBook(libraryService);
                            }else{
                                System.out.println("感谢使用图书馆系统！");
                                return;
                            }
                            break;
                        case 5:
                            if(user instanceof AdminUser){
                                System.out.println("感谢使用图书馆系统！");
                                return;
                            }
                        default:
                            System.out.println("无效的选择，请重新输入。");
                    }
                } catch (NumberFormatException e){
                    System.out.println("无效的输入，请输入数字选项。");
                }
            }
        }
    }

    private static void addBook(Scanner scanner,LibraryService libraryService){
        System.out.println("请输入书名:");
        String title = scanner.nextLine().trim();
        System.out.println("请输入作者:");
        String author = scanner.nextLine().trim();
        System.out.println("请输入价格:");
        Double price = scanner.nextDouble();
        scanner.nextLine();
        System.out.println("请输入类型:");
        String type = scanner.nextLine().trim();
        libraryService.addBook(new entities.Book(title,author,price,type));
    }

    private static void borrowBook(Scanner scanner,LibraryService libraryService){
        System.out.println("请输入书名:");
        String title = scanner.nextLine().trim();
        libraryService.borrowBook(title);
    }

    private static void removeBook(Scanner scanner,LibraryService libraryService){
        System.out.println("请输入书名:");
        String title = scanner.nextLine().trim();
        libraryService.removeBook(title);
    }

    private static void returnBook(Scanner scanner,LibraryService libraryService){
        System.out.println("请输入书名:");
        String title = scanner.nextLine().trim();
        libraryService.returnBook(title);
    }

    private static void listAllBook(LibraryService libraryService){
        System.out.println("所有图书列表:");
        for(entities.Book book : libraryService.getAllBooks()){
            System.out.println(book);
        }
    }
}